//
//  UIStackViewViewController2.swift
//  UIKitDemoApp
//
//  Created by ROBIN.J.Y.ZHONG on 2023/8/25.
//

import UIKit

class UIStackViewViewController2: BaseViewController {
    private lazy var stackV: UIStackView = {
        let view = UIStackView()
        view.axis = .vertical
        view.distribution = .fill
        view.spacing = 10
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()

    private lazy var stackV1: UIStackView = {
        let view = UIStackView()
        view.axis = .horizontal
        view.distribution = .fillProportionally // 按比例分配
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()
    
    private lazy var leftLabel1: UILabel = {
        let lb = UILabel()
        lb.text = "LeftLeftLeftLeftLeftLeftLeftLeftLeftLeftLeftLeftLeftLeft"
        lb.backgroundColor = .yellow
        return lb
    }()
    
    private lazy var rightLabel1: UILabel = {
        let lb = UILabel()
        lb.text = "RightRightRight"
        lb.backgroundColor = .systemPink
        return lb
    }()
    
    private lazy var stackV2: UIStackView = {
        let view = UIStackView()
        view.axis = .horizontal
        view.distribution = .fillProportionally // 按比例分配
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()
    
    private lazy var leftLabel2: UILabel = {
        let lb = UILabel()
        lb.text = "LeftLeftLeftLeftLeftLeftLeftLeftLeftLeftLeftLeftLeftLeft"
        lb.backgroundColor = .yellow
        return lb
    }()
    
    private lazy var rightLabel2: UILabel = {
        let lb = UILabel()
        lb.text = "RightRightRightRightRightRightRightRightRightRightRightRight"
        lb.backgroundColor = .systemPink
        // 为了让它不被压缩，设置抗压缩
        // .required : 要求空间一定要足够显示内容， 如果屏幕宽度不够的情况下，会出现挤占其他元素空间，导致其他元素不显示
        lb.setContentCompressionResistancePriority(.required, for: .horizontal)
        
        // 还有一个属性抗拉伸, 与 setContentCompressionResistancePriority 类似
        // lb.setContentHuggingPriority(.required, for: .horizontal)
        return lb
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "UIStackView-栈视图-按比例分配"
        
        setupViews()
    }

    private func setupViews() {
        stackV1.addArrangedSubview(leftLabel1)
        stackV1.addArrangedSubview(rightLabel1)
        
        stackV2.addArrangedSubview(leftLabel2)
        stackV2.addArrangedSubview(rightLabel2)
        
        stackV.addArrangedSubview(stackV1)
        stackV.addArrangedSubview(stackV2)
        
        view.addSubview(stackV)
        
        NSLayoutConstraint.activate([
            stackV.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            stackV.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            stackV.topAnchor.constraint(equalTo: view.topAnchor)
        ])
    }
}
